//
//  DeviceHandleView.swift
//  YitihuaPH
//
//  Created by Kai Kang on 2019/1/23.
//  Copyright © 2019 Kai Kang. All rights reserved.
//

import UIKit

class DeviceHandleView: KBaseCustomWindow {
    
    var tableView: UITableView!
    var dataSource = [String]()
    var selected: ((_ index: NSInteger) -> Void)?
    
    
    override func subViewInit() {
        super.subViewInit()
        
        dataSource = ["配置时间",
                      "配置IP",
                      "配置蓝牙名称",
                      "配置硬件编码",
                      "查询IP",
                      "查询状态",
                      "配置平衡参数",
                      "初始化平衡角度",
                      "查询平衡参数",
                      "配置定时上报时间间隔",
                      "配置蓝牙等待时间间隔",
                      "开锁",
                      "重启"]
        
        contentView.height = height * 0.8
        contentView.width = width
        popStyle = .bottom
        
        tableView = UITableView.init(frame: contentView.bounds)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.contentInset = UIEdgeInsets.init(top: 10, left: 0, bottom: 10, right: 0)
        contentView.addSubview(tableView)
    }
}

extension DeviceHandleView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "CELL") as? DevicehandleCell
        if cell == nil {
            cell = DevicehandleCell.init(style: .default, reuseIdentifier: "CELL")
        }
        cell?.handleNamelabel.text = dataSource[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let selected = self.selected {
            selected(indexPath.row)
            hide()
        }
    }
}


class DevicehandleCell: UITableViewCell {
    
    var handleNamelabel: UILabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.accessoryType = .none
        handleNamelabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
        handleNamelabel.textColor = UIColor.black
        handleNamelabel.textAlignment = .center
        handleNamelabel.adjustsFontSizeToFitWidth = true
        handleNamelabel.minimumScaleFactor = 0.5
        self.contentView.addSubview(handleNamelabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
